
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )


// This is the spawn function. It's called when a client calls the entity to be spawned.
// If you want to make your SENT spawnable you need one of these functions to properly create the entity
//
// ply is the name of the player that is spawning it
// tr is the trace from the player's eyes 
//
function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "sent_flak" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

function ENT:SetPlayer(ply)

	self.Entity:SetVar( "Founder", ply )
	self.Entity:SetPhysicsAttacker(ply)
	self.Entity:SetOwner(ply)
    self.Entity:SetVar( "FounderIndex", ply:UniqueID() )
    
    self.Entity:SetNetworkedString( "FounderName", ply:Nick() )
	//print(self.Entity:GetNetworkedString( "FounderName" ))

end

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self.Entity:SetModel( "models/items/combine_rifle_ammo01.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS ) 
	self.Entity:SetKeyValue("rendercolor", "25 25 25")
	self.Entity:SetColor(30,30,30,255)
	self.Entity:SetMaterial("props_pipes/valve001_skin2")
	self.Entity:SetGravity(.1)
	
	self.ETimer = CurTime() + (math.random(4,38)/10)
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableGravity(false)
		phys:EnableCollisions(true)
		phys:SetMass(2)
	end
	
end

function FlakExplode(self)

	local pos = self:GetPos()
	local tracedata = {}
	tracedata.start = pos
	tracedata.endpos = pos+Vector(0,0,-90)
	local trace = util.TraceLine(tracedata)
	if trace.HitWorld then
		util.Decal("Scorch",trace.HitPos+trace.HitNormal,trace.HitPos-trace.HitNormal)
	end

	local flaksounds = {
	"ambient/explosions/exp1.wav",
	"ambient/explosions/exp2.wav",
	"ambient/explosions/exp3.wav",
	"ambient/explosions/explode_4.wav",
	"ambient/explosions/explode_9.wav",
	"weapons/underwater_explode3.wav",
	"weapons/underwater_explode4.wav"}
	
	local debrist = {
	"ambient/levels/streetwar/city_battle14.wav",
	"ambient/levels/streetwar/city_battle15.wav",
	"ambient/levels/streetwar/city_battle17.wav",
	"ambient/levels/streetwar/city_battle18.wav",
	"ambient/levels/streetwar/city_battle19.wav",
	"ambient/levels/streetwar/city_battle6.wav",
	"ambient/levels/streetwar/city_battle7.wav",
	"ambient/levels/streetwar/city_battle11.wav"}
	
	local sn = Sound(flaksounds[math.random(1,7)])
	local debris = Sound(debrist[math.random(1,8)])
	local rando = math.random(1,6)
	util.PrecacheSound(sn)
	util.PrecacheSound(debris)
	
	self:EmitSound(sn,math.random(70,100),math.random(50,150))
	if rando == 6 then
		self:EmitSound(debris,math.random(60,80),math.random(60,150))
	end
	
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos())
	util.Effect("flak_explode", effectdata)
	
	local power = CheckSurroundings(self)
	
	util.BlastDamage(self,self:GetOwner(),self:GetPos(),50 + (power*2),power)
	
	for k,v in pairs(ents.FindInSphere( self:GetPos(), 50 + (power*2) )) do
	
		if v != self then
			local hforce = (v:GetPos() - self:GetPos()):Normalize()
			local phys = v:GetPhysicsObject()
			if phys:IsValid() then
				phys:ApplyForceCenter(hforce * (phys:GetMass()/1.5))
			end
			v:EmitSound(debris,math.random(65,75),math.random(80,120))
		end
	end
	
	//self:Remove()  This causes console spam. :/
	
	self:Fire("kill",1,0.01)

end

function CheckSurroundings(self)

	local pos = self:GetPos()
	local dist = -100
	local ep = Vector(0,0,dist)
	local power = 20
	local send = false
	
	while send == false do
	
		local pos = self:GetPos()
		local ep = Vector(0,0,dist)
		local tracedata = {}
		tracedata.start = pos
		tracedata.endpos = pos+ep
		local trace = util.TraceLine(tracedata) 
		
		if trace.HitWorld then 
			send = true 
		elseif trace.HitNonWorld then
			power = power + 30 or 60
			send = true
		else
			dist = dist*2
			power = power + math.random(4,9)
			send = false
		end
		if power >= 200 then send = true end
	end
	return power
end

/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function ENT:Think()

	if self.Entity:WaterLevel() > 0 then
		FlakExplode(self.Entity)
	end

	self.ETimer = self.ETimer or CurTime() + (math.random(5,35)/10)
 
    if (self.ETimer < CurTime()) then
    FlakExplode(self.Entity)
    self.ETimer = nil
    end
  
 end 


/*---------------------------------------------------------
   Name: PhysicsCollide
---------------------------------------------------------*/
function ENT:PhysicsCollide( data, physobj )
	FlakExplode(self.Entity)
end